import { Subject } from 'rxjs';
import { Injectable } from '@angular/core';
import { v4 as UUID } from 'uuid';
@Injectable({
  providedIn: 'root'
})
export class EventManagerService {

  menuChange$: Subject<any> = new Subject<any>();
  export$: Subject<any> = new Subject<any>();

  constructor() { }

  private map = new Map<string, (args: any) => void>();

  registEvent(eventName: string, handle) {
    const keyId = eventName + '#' + UUID().replace(/-/g, '');
    this.map.set(keyId, handle);
    return keyId;
  }

  callEvents(eventName: string, args) {
    const keys = this.map.keys();
    for (const item of keys) {
      const t = item.split('#');
      if (eventName == t[0]) {
        const hd = this.map.get(item);
        hd(args);
      }
    }
  }

  removeEvent(eventName: string) {
    this.map.delete(eventName);
  }

  tableDataExport(params: any) {
    this.export$.next(params);
  }
}
